<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="h_top">
          <div class="bread_title">
            <span>更新工装</span>
          </div>
          <div class="save_button">
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-back" size="mini" @click="goBack">返回</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-checked" size="mini" @click="save">保存</el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-row>
        <el-col :span="24">
          <el-form ref="form" :model="data" :inline="true" :rules="formRules">
            <el-input v-model="data.id" type="hidden" />
            <el-row :gutter="10">
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item prop="coding">
                  <div>编号</div>
                  <el-input v-model="data.coding" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item>
                  <div>名称</div>
                  <el-input v-model="data.name" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item>
                  <div>图号</div>
                  <el-input v-model="data.spec" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item>
                  <div>成本中心</div>
                  <v-select v-model="data.cost_id" action="costList" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="7" :sm="8">
                <el-form-item>
                  <div>供应商</div>
                  <v-select v-model="data.provider_id" action="onlyProviderList" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="7" :sm="8">
                <el-form-item>
                  <div>制造商</div>
                  <v-select v-model="data.manufacturer_id" action="onlyManufacturerList" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="5" :sm="8">
                <el-form-item>
                  <div>状态</div>
                  <el-select v-model="data.status" placeholder="请选择" size="mini">
                    <el-option v-for="(item, index) in eqStatus" :key="index" :label="item" :value="index" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="5" :sm="8">
                <el-form-item>
                  <div>出厂编号</div>
                  <el-input v-model="data.serialize" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item>
                  <div>所属车间</div>
                  <v-select v-model="data.workshop" action="workshopList" :query="{factory_id:data.factory}" @change="clearLine" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item>
                  <div>所属生产线</div>
                  <v-select v-model="data.line" action="lineList" :query="{workshop_id:data.workshop}" @change="clearProcess" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item>
                  <div>所属工位</div>
                  <v-select v-model="data.process" action="processList" :query="{line_id:data.line}" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item>
                  <div>生产日期</div>
                  <el-date-picker v-model="data.purchase_date" type="date" size="mini" value-format="yyyy-MM-dd" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item>
                  <div>购置日期</div>
                  <el-date-picker v-model="data.manufacture_date" type="date" size="mini" value-format="yyyy-MM-dd" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item>
                  <div>购置价格</div>
                  <el-input v-model="data.price" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item>
                  <div>维护负责人</div>
                  <v-select v-model="data.admin_id" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8" v-if="data.admin">
                <el-form-item>
                  <div>维护负责人电话</div>
                  <el-input v-model="data.admin.phone" size="mini" :disabled="true" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item>
                  <div>排序</div>
                  <el-input v-model="data.sort" size="mini" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <el-tabs>
            <el-tab-pane label="描述">
              <el-row :gutter="10">
                <el-col :span="4">
                  <div>
                    <span>创建时间日期</span>
                    <el-input v-model="data.create_time" placeholder="创建时间日期" class="mtb10" size="mini" :disabled="true" />
                  </div>
                  <div>
                    <span>创建人</span>
                    <el-input v-model="data.cr_admin.name" placeholder="创建人" class="mtb10" size="mini" :disabled="true" />
                  </div>
                  <div v-if="data.update_time">
                    <div>
                      <span>最近更新</span>
                      <el-input v-model="data.update_time" placeholder="最近更新" class="mtb10" size="mini" :disabled="true" />
                    </div>
                    <div>
                      <span>更新者</span>
                      <el-input v-model="data.up_admin.name" placeholder="更新者" class="mtb10" size="mini" :disabled="true" />
                    </div>
                  </div>
                </el-col>
                <el-col :span="16">
                  <Tinymce id="content" v-model="data.content" />
                </el-col>
                <el-col :span="4">
                  <thumb v-model="data.thumb" />
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane lazy label="关联备件">
              <template>
                <el-table :data="parts_list" :stripe="true">
                  <el-table-column min-width="15%" label="备件" prop="parts.name" />
                  <el-table-column min-width="65%" label="注意" prop="notice" />
                  <el-table-column min-width="20%" label="品牌" prop="parts.brand" />
                </el-table>
              </template>
            </el-tab-pane>
            <el-tab-pane lazy label="维修备件">
              <template>
                <div class="part">
                  <el-table :data="cm_parts_list">
                    <el-table-column label="日期" prop="create_time" />
                    <el-table-column label="工单" prop="wordorder">
                      <template slot-scope="{row}">{{ row.coding }} - {{ row.name }}</template>
                    </el-table-column>
                    <el-table-column label="备品备件">
                      <template slot-scope="{row}">
                        <div v-for="(item, index) in row.used_parts_list" :key="index">
                          <p>{{ item.parts.name }}</p>
                        </div>
                      </template>
                    </el-table-column>
                    <el-table-column label="出库数量">
                      <template slot-scope="{row}">
                        <div v-for="(item, index) in row.used_parts_list" :key="index">
                          <p>{{ item.number }}{{ item.parts.unit }}</p>
                        </div>
                      </template>
                    </el-table-column>
                    <el-table-column label="单价 (¥)">
                      <template slot-scope="{row}">
                        <div v-for="(item, index) in row.used_parts_list" :key="index">
                          <p>{{ item.parts.price }}元</p>
                        </div>
                      </template>
                    </el-table-column>
                    <el-table-column label="总价 (¥)" prop="total_price">
                      <template slot-scope="{row}">
                        <div v-for="(item, index) in row.used_parts_list" :key="index">
                          <p>{{ item.parts.price * item.number }}元</p>
                        </div>
                      </template>
                    </el-table-column>
                  </el-table>
                </div>
              </template>
            </el-tab-pane>
            <!--<el-tab-pane lazy label="参数">-->
            <!--<equipment-parameter v-model="data.id" />-->
            <!--</el-tab-pane>-->
            <!--<el-tab-pane lazy label="文档|附件">-->
            <!--<equipment-document v-model="data.id" />-->
            <!--</el-tab-pane>-->
            <el-tab-pane lazy label="维护计划">
              <el-table :data="plan_list">
                <el-table-column label="编码" prop="coding" />
                <el-table-column label="名称" prop="name" />
                <el-table-column label="优先程度" prop="priority">
                  <template v-slot="{row}">{{ priority[row.priority] }}</template>
                </el-table-column>
                <el-table-column label="类别" prop="category">
                  <template v-slot="{row}">{{ planCategory[row.category] }}</template>
                </el-table-column>
                <el-table-column label="专业" prop="profession">
                  <template v-slot="{row}">{{ profession[row.profession] }}</template>
                </el-table-column>
                <el-table-column label="安排时间" prop="arrange_date" />
              </el-table>
            </el-tab-pane>
            <!--<el-tab-pane lazy label="仪器送检">-->
            <!--<equipment-inspect v-model="data.inspect" @inspect="inspect" />-->
            <!--</el-tab-pane>-->
            <!--<el-tab-pane lazy label="历史">-->
            <!--<equipment-history v-model="data.id" />-->
            <!--</el-tab-pane>-->
            <el-tab-pane lazy label="故障树">
              <template>
                <el-table :data="failure_list">
                  <el-table-column label="部位" prop="parts" />
                  <el-table-column label="次数" prop="times" />
                </el-table>
              </template>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import API from '@/api/asset/jig'
import { attribute, weight, fun, eqStatus, priority, planCategory, profession } from '@/utils/options'
import Tinymce from '@/components/Tinymce/index'
import thumb from '@/components/Upload/thumb'
import components from '@/components/asset/equipment/index'
import VSelect from '@/components/select'
export default {
  inject: ['backUrl', 'delOne', 'update', 'appendOtherAdmin', 'delOtherAdmin', 'getWeightFormat', 'goUrl'],
  components: {
    Tinymce,
    thumb,
    VSelect,
    ...components
  },
  data() {
    return {
      data: {
        'admin': {
          'phone': ''
        },
        'cr_admin': {
          'name': ''
        }
      },
      parts_list: [],
      part_list: [],
      cm_parts_list: [],
      plan_list: [],
      failure_list: [],
      attribute,
      weight,
      fun,
      eqStatus,
      priority,
      planCategory,
      profession,
      formRules: {}
    }
  },
  watch: {
  },
  created() {
    const id = this.$route.params.id
    API.getOne(id).then(response => {
      if (response.data.error_code === 24404) {
        this.$alert('您请求的分类不存在', '错误', {
          confirmButtonText: '确定',
          callback: () => {
            this.backUrl()
          }
        })
      }
      console.log(response.data.pm_list)
      if (response.data.failure_list.length !== 0) {
        const failList = []
        for (const key in response.data.failure_list) {
          const temp = {}
          temp.parts = key
          temp.times = response.data.failure_list[key]
          failList.push(temp)
        }
        this.failure_list = failList
      }
      this.data = response.data
      this.parts_list = response.data.jig_part
      this.cm_parts_list = response.data.used_parts_list
      this.plan_list = response.data.pm_list
    })
  },
  methods: {
    inspect(value) {
      this.$set(this.data, 'inspect', value)
    },
    confirmDel() {
      const id = this.data.id
      this.delOne(API, id, true)
    },
    save() {
      const thisData = this.data
      const thisForm = this.$refs.form
      this.update(API, thisData, thisForm)
    },
    append() {
      const thisForm = this.$refs.append_form
      const admin_data = {
        'admin_id': this.data.admin_id,
        'category': this.$route.params.id,
        'weight': this.data.weight
      }
      const errorCode = 92004
      const errorMessage = '该人员已存在'
      this.appendOtherAdmin(API, thisForm, admin_data, errorCode, errorMessage)
    },
    otherAdminDel(admin_id) {
      const admin_data = {
        'admin_id': admin_id,
        'category': this.$route.params.id
      }
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.delOtherAdmin(admin_data).then((response) => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            API.category(this.$route.params.id).then(response => {
              this.list = response.data.other_admin
            })
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    weightFormat(row) {
      return this.getWeightFormat(row)
    },
    clearLine() {
      this.data.line = null
    },
    clearProcess() {
      this.data.process = null
    },
    goBack() {
      this.goUrl('/asset/jig')
    }
  }
}
</script>
<style>
.mtb10{
  margin: 10px 0;
}
</style>
